package com.aigou.admin.controller;

import com.aigou.admin.annotation.ResponseResult;
import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.Product;
import com.aigou.admin.entity.vo.ResponseVo;
import com.aigou.admin.service.ProductService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Hor
 * @Date: 2024/12/2 19:16
 * @Version: 1.0
 */

@RestController
@RequestMapping("/product")
@AllArgsConstructor
@ResponseResult
public class ProductController {

    private final ProductService productService;

    @RequestMapping("/save")
    public ResponseVo save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String product_name = req.getParameter("product_name");
        String product_image = req.getParameter("product_image");
        Double price = Double.valueOf(req.getParameter("price"));
        String product_type = req.getParameter("product_type");
        String product_desc = req.getParameter("product_desc");
        String product_brand = req.getParameter("product_brand");
        Product product = new Product(id,
                product_name,
                product_image,
                price,
                product_type,
                product_desc,
                null,
                product_brand);
        Boolean save = productService.save(product);
        return ResponseVo.buildSuccess(save);
    }

    @RequestMapping("/findById")
    public ResponseVo findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获得id
        String id = req.getParameter("id");
        // 查询商品
        Product product = productService.findById(id);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(product);
    }

    @RequestMapping("/findByName")
    public ResponseVo findByName(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        // 获得id
        String product_name = req.getParameter("product_name");
        // 查询商品
        List<Product> product = productService.findByName(product_name);
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(product);
    }

    @RequestMapping("/findAll")
    public ResponseVo findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询用户
        List<Product> users = productService.findAll();
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(users);
    }


    @RequestMapping("/delete")
    public ResponseVo delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Boolean remove = productService.remove(id);
        return ResponseVo.buildSuccess(remove);
    }

    @RequestMapping("/update")
    public ResponseVo update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String product_name = req.getParameter("product_name");
        String product_image = req.getParameter("product_image");
        Double price = Double.valueOf(req.getParameter("price"));
        String product_type = req.getParameter("product_type");
        String product_desc = req.getParameter("product_desc");
        String product_brand = req.getParameter("product_brand");

        Boolean update = productService.update(new Product(id,
                product_name,
                product_image,
                price,
                product_type,
                product_desc,null,
                product_brand));
        return ResponseVo.buildSuccess(update);

    }

    @RequestMapping("/page")
    public ResponseVo page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        //查询用户
        PageEntity<Product> productByPage = productService.findProductByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        //转换为JSON,通过流发送给客户端
        return ResponseVo.buildSuccess(productByPage);
    }

}
